/**
 * 
 */
package com.desksoft.ecommerce.handler.controller;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.ServletContextAware;

import com.desksoft.ecommerce.handler.listener.SessionListener;
import com.desksoft.ecommerce.helper.MessageHelper;
import com.desksoft.ecommerce.jms.NotifyServFactory;
import com.desksoft.ecommerce.util.result.ServiceResult;
import com.google.common.collect.Maps;

/***************************************
 * 版权所有：杭州迪科软件 保留所有权利
 * 创建日期: 
 * 创建作者：yejw
 * 文件名称：
 * 版本：
 * 功能：
 * 最后修改时间：
 * 修改记录：
 *****************************************/
public abstract class AbstractController implements ServletContextAware, IGlobalPages, IGlobalActions {

	private ServletContext servletContext;
	
	/**
	 * 获取消息工厂实例
	 * 
	 * @return NotifyFactory
	 */
	protected NotifyServFactory getNotifyServ() {
		return NotifyServFactory.getInstance();
	}
	
	/**
	 * 根据 sessionId 取 SESSION
	 * 
	 * @deprecated
	 * @param sessionId
	 * @return
	 */
	protected HttpSession getSession(String sessionId) {
		return SessionListener.getSession(this.servletContext, sessionId);
	}
	
	/**
	 * 加入服务调用失败信息
	 * 
	 * @param <T>
	 * @param modelMap
	 * @param servResult
	 */
	protected <T> void failMessage(ModelMap modelMap, ServiceResult<T> servResult) {
        this.failMessage(modelMap, servResult.getResourceMsg());
    }
	
	/**
	 * 加入失败代码信息
	 * 
	 * @param <T>
	 * @param modelMap
	 * @param code
	 * @param args
	 */
	protected <T> void failCodeMessage(ModelMap modelMap, String code, Object ... args) {
		this.failMessage(modelMap, MessageFormat.format(MessageHelper.get(code), args));
    }
	
	/**
	 * 加入成功代码信息
	 * 
	 * @param <T>
	 * @param code
	 */
	protected <T> void succCodeMessage(ModelMap modelMap, String code, Object ... args) {
		this.succMessage(modelMap, MessageFormat.format(MessageHelper.get(code), args));
    }
	
	/**
	 * 加入成功信息
	 * 
	 * @param <T>
	 * @param modelMap
	 */
	protected <T> void succMessage(ModelMap modelMap) {
		modelMap.put("success", Boolean.TRUE);
	}
	/**
	 * 加入成功信息
	 * 
	 * @param <T>
	 * @param modelMap
	 * @param message
	 */
	protected <T> void succMessage(ModelMap modelMap, T message) {
		modelMap.put("success", Boolean.TRUE);
		modelMap.put("detail", message);
	}
	
	/**
	 * 加入失败信息
	 * 
	 * @param <T>
	 * @param modelMap
	 */
	protected <T> void failMessage(ModelMap modelMap) {
		modelMap.put("success", Boolean.FALSE);
	}
	
	/**
	 * 加入失败信息
	 * 
	 * @param <T>
	 * @param modelMap
	 * @param message
	 */
	protected <T> void failMessage(ModelMap modelMap, T message) {
		modelMap.put("success", Boolean.FALSE);
		modelMap.put("detail", message);
	}
	
	@InitBinder  
    protected void initBinder(WebDataBinder binder) {  
        SimpleDateFormat dateFormat = new SimpleDateFormat(getDateFormatPattern());  
        dateFormat.setLenient(false);  
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat, false));  
    }  
	
	/**
	 * 返回失败代码信息
	 * 
	 * @param <T>
	 * @param code
	 * @param args
	 */
	protected Map<String, Object> failCodeMessage(String code, Object ... args) {
		return this.failMessage(MessageFormat.format(MessageHelper.get(code), args));
    }
	
	/**
	 * 返回成功代码信息
	 * 
	 * @param <T>
	 * @param code
	 */
	protected Map<String, Object> succCodeMessage(String code, Object ... args) {
		return this.succMessage(MessageFormat.format(MessageHelper.get(code), args));
    }
	
	/**
	 * 返回成功信息
	 * 
	 * @param <T>
	 */
	protected Map<String, Object> succMessage() {
		return this.succMessage("");
	}
	
	/**
	 * 返回成功信息
	 * 
	 * @param <T>
	 * @param message
	 */
	protected Map<String, Object> succMessage(Object message) {
		Map<String, Object> result = Maps.newHashMap();
		
		result.put("success", Boolean.TRUE);
		result.put("detail", message);
		
		return result;
	}
	
	/**
	 * 返回成功编码及对应的参数
	 * @param code
	 * @param param
	 * @return
	 */
	protected Map<String, Object> succCodeWithParam(String code, Object param) {
		Map<String, Object> result = this.succMessage(code);
		result.put("params", param);
		
		return result;
	}
	
	/**
	 * 返回失败信息
	 * 
	 * @param <T>
	 */
	protected Map<String, Object> failMessage() {
		return this.failMessage("");
	}
	
	/**
	 * 返回失败编码及对应的参数
	 * @param code
	 * @param param
	 * @return
	 */
	protected Map<String, Object> failCodeWithParam(String code, Object param) {
		Map<String, Object> result = this.failMessage(code);
		result.put("params", param);
		
		return result;
	}
	
	/**
	 * 返回失败信息
	 * 
	 * @param <T>
	 * @param message
	 */
	protected Map<String, Object> failMessage(Object message) {
		Map<String, Object> result = Maps.newHashMap();
		
		result.put("success", Boolean.FALSE);
		result.put("detail", message);
		
		return result;
	}
	
	/**
	 * 日期格式
	 * 
	 * @return
	 */
	protected abstract String getDateFormatPattern();

	public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
	
	protected ServletContext getServletContext() {
        return servletContext;
    }
	
}
